package qeb.handheldmedical.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.LRLayout;
import qeb.handheldmedical.R;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2017年2月23日 下午1:13:31
 * @declaration
 */
public class CheckInstructionItemAdapter extends BaseAdapter {
	// private ImageSwitcher mImageLoader;
	private int mCount;
	private Context mContext;
	private JSONArray jsonArray = null;
	private String sState;// 2是最后输液

	public CheckInstructionItemAdapter(Context context, JSONArray jsonArray) {
		this.mCount = jsonArray.length();
		this.mContext = context;
		this.jsonArray = jsonArray;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_check_instruction_item, null);

			viewholder = new ViewHolder();

			viewholder.tvYzmc = (TextView) convertView
					.findViewById(R.id.tvYzmc);
//			viewholder.tvYzbz = (TextView) convertView
//					.findViewById(R.id.tvYzbz);
			viewholder.tvYzjl = (TextView) convertView
					.findViewById(R.id.tvYzjl);
			viewholder.tvZxsj = (TextView) convertView
					.findViewById(R.id.tvZxsj);
			viewholder.tvTz = (TextView) convertView
					.findViewById(R.id.tvTz);
			viewholder.tvGyfs = (TextView) convertView
					.findViewById(R.id.tvGyfs);
			viewholder.tvZxjssj = (TextView) convertView
					.findViewById(R.id.tvZxjssj);
			viewholder.llRoot = (LinearLayout) convertView
					.findViewById(R.id.llRoot);
			viewholder.tvPl = (TextView) convertView
					.findViewById(R.id.tvPl);
			viewholder.tvFs = (TextView) convertView.findViewById(R.id.tvFs);
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		try {
//			JSONObject prveYzObj = null;
//			String fyzId = "";
//			if (position != 0) {
//				prveYzObj = jsonArray.getJSONObject(position - 1);
//				fyzId = prveYzObj.getString("FYZID");
//			}
//
//			JSONObject yz = jsonArray.getJSONObject(position);
//			if (yz != null) {
//				String strZfbz = yz.getString("ZFBZ");// 99 最后执行输液
//				if (strZfbz.equals("0") || strZfbz.equals("99"))
//					strZfbz = "";
//				// 医嘱执行状态  1已结束 其他
//				if (yz.getString("ZXSTATEID").equals("1")) {
//					convertView.setBackgroundColor(Color.GREEN);
//				} else {
//					convertView.setBackgroundColor(Color.WHITE);
//				}
//
//				String strIsGwyp = yz.getString("SFGWYP");// 1:是
//				String strIsPsyp = yz.getString("SFPSSX");// 1:是
//				// 用药频率
//				if (yz.getString("FREQU_CODE").equals("立即使用")) {
//					viewholder.tvYzmc.setText(Html.fromHtml(yz
//							.getString("YZMC").replace("[新]",
//									"<font color='red'>[新]</font>")
//							+ yz.getString("YPGG")));
//				} else {
//
//					viewholder.tvYzmc.setText(yz.getString("YZMC")
//							+ yz.getString("YPGG"));
//				}
//				viewholder.tvYzbz.setText("备注:"
//						+ (yz.getString("PSJG").equals("null") ? "" : yz
//								.getString("PSJG")) + strZfbz);
//
//				viewholder.tvYzjl.setText("剂量:" + yz.getString("DCJL")
//						+ yz.getString("JLDW") + " "
//						+ yz.getString("FREQU_CODE"));
//				viewholder.tvZxsj.setText("执行时间:"
//						+ (yz.getString("ZXSJ").equals("null") ? "" : yz
//								.getString("ZXSJ")));
//				viewholder.tvFs
//						.setText((yz.getString("GYFS").equals("GYFS") ? "" : yz
//								.getString("GYFS")));
//
//				viewholder.tvYzmc.setTextColor(Color.BLACK);
//				viewholder.tvYzbz.setTextColor(Color.BLACK);
//				viewholder.tvYzjl.setTextColor(Color.BLACK);
//				viewholder.tvZxsj.setTextColor(Color.BLACK);
//				viewholder.tvFs.setTextColor(Color.BLACK);
//
//				if (yz.getString("FYBZ").equals("0")
//						|| strZfbz.indexOf("拒用") != -1
//						|| strZfbz.indexOf("按医嘱不执行") != -1) {
//					convertView.setPadding(50, 0, 30, 0);
//					viewholder.tvYzmc.setTextColor(color.WhiteSmoke);
//					viewholder.tvYzbz.setTextColor(color.WhiteSmoke);
//					viewholder.tvYzjl.setTextColor(color.WhiteSmoke);
//					viewholder.tvZxsj.setTextColor(color.WhiteSmoke);
//					viewholder.tvFs.setTextColor(color.WhiteSmoke);
//				} else {
//					convertView.setPadding(10, 0, 10, 0);
//					//非副药，且长期医嘱是新字体为灰色
//					if (!yz.getString("FREQU_CODE").equals("立即使用")){
//						 if(yz.getString("YZMC").indexOf("[新]")!=-1){
//							viewholder.tvYzmc.setTextColor(color.WhiteSmoke);
//							viewholder.tvYzbz.setTextColor(color.WhiteSmoke);
//							viewholder.tvYzjl.setTextColor(color.WhiteSmoke);
//							viewholder.tvZxsj.setTextColor(color.WhiteSmoke);
//							viewholder.tvFs.setTextColor(color.WhiteSmoke);
//						}
//					}
//				}
//				if (strIsGwyp.equals("1") || strIsPsyp.equals("1")) {
//					viewholder.tvYzmc.setTextColor(Color.RED);
//					viewholder.tvYzbz.setTextColor(Color.RED);
//					viewholder.tvYzjl.setTextColor(Color.RED);
//					viewholder.tvZxsj.setTextColor(Color.RED);
//					viewholder.tvFs.setTextColor(Color.RED);
//				}
//			}
			JSONObject yz = jsonArray.getJSONObject(position);
			if(yz != null){
					String key = yz.getString("key");
					JSONArray value = yz.getJSONArray("value");
                    JSONObject firstItem = value.getJSONObject(0);
                    if(firstItem != null){
                        String yztz = firstItem.getString("YZTZ");
                        if("yes".equals(yztz)){
                            // 今日停止
                            viewholder.tvTz.setVisibility(View.VISIBLE);
                        }else{
                            // 不是今日停止
                            viewholder.tvTz.setVisibility(View.GONE);
                        }
                    }
                if("kfyw".equals(key)){
                    // 口服
                    viewholder.llRoot.removeAllViews();
                    JSONObject jsonObject = value.getJSONObject(0);
                    if(jsonObject != null) {
                        LRLayout lrLayout = setLayout(convertView, viewholder, jsonObject);
                        viewholder.llRoot.addView(lrLayout);
                    }

                }else {
                    // 其他
                        viewholder.llRoot.removeAllViews();
                        for (int i1 = 0; i1 < value.length(); i1++) {
                            JSONObject jsonObject = value.getJSONObject(i1);
                            LRLayout lrLayout = setLayout(convertView, viewholder, jsonObject);
                            viewholder.llRoot.addView(lrLayout);
                        }

                }
//

//				// 用药剂量
//				String dose =  getUsableValue(yz.getString("DOSE"));
//				// 用药单位
//				String dose_unit = getUsableValue(yz.getString("DOSE_UNIT"));
				// 用药频率
//				String frequency = getUsableValue(yz.getString("FREQUENCY"));
				// 药品名称 以及 规格
//				viewholder.tvYzmc.setText(yz.getString("ORDER_NAME"));
//				viewholder.tvYzjl.setText(dose+dose_unit+" "+frequency);
				// 格式化返回不规范的执行时间
//				viewholder.tvZxsj.setText("执行时间:"+getForMatTime(yz.getString("ZXCZSJ")));
				// 给药方式
//				viewholder.tvFs.setText(yz.getString("SUPPLY_NAME"));

			}
		} catch (Exception e) {
			// TODO: handle exception

		}
		return convertView;
	}

    @NonNull
    private LRLayout setLayout(View convertView, ViewHolder viewholder, JSONObject jsonObject) throws JSONException {
        setPl(viewholder, jsonObject);
        LRLayout lrLayout = content(convertView, jsonObject);
        setSj(viewholder, jsonObject);
        return lrLayout;
    }

    @NonNull
    private LRLayout content(View convertView, JSONObject jsonObject) throws JSONException {
        LRLayout lrLayout = new LRLayout(convertView.getContext());
        // 用药剂量
        String dose = getUsableValue(jsonObject.getString("DOSE"));
        // 用药单位
        String dose_unit = getUsableValue(jsonObject.getString("DOSE_UNIT"));
        lrLayout.left(jsonObject.getString("ORDER_NAME"));
        lrLayout.right(dose + dose_unit);
        return lrLayout;
    }

    private void setPl(ViewHolder viewholder, JSONObject jsonObject) throws JSONException {
        viewholder.tvGyfs.setText(jsonObject.getString("SUPPLY_NAME"));
        viewholder.tvPl.setText(jsonObject.getString("FREQUENCY"));
    }

    private void setSj(ViewHolder viewholder, JSONObject jsonObject){
        String yzzxbz = null;
        try {
            yzzxbz = getUsableValue(jsonObject.getString("YZZXBZ"));
            if("1".equals(yzzxbz)){
                String zxczsj = jsonObject.getString("ZXCZSJ");
                if(!TextUtils.isEmpty(zxczsj)){
                    viewholder.tvZxsj.setVisibility(View.VISIBLE);
                    viewholder.tvZxsj.setText("开始时间 "+zxczsj);
                }
            }else if("2".equals(yzzxbz)){
                String zxczjssj = jsonObject.getString("ZXCZJSSJ");
                if(!TextUtils.isEmpty(zxczjssj)) {
                    viewholder.tvZxjssj.setVisibility(View.VISIBLE);
                    viewholder.tvZxjssj.setText("结束时间 " + zxczjssj);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void setBackGroud(JSONObject jsonObject,View convertView) {
        try {
            if (("2".equals(jsonObject.getString("YZZXBZ")))) {
                convertView.setBackgroundColor(Color.GREEN);
            } else {
                convertView.setBackgroundColor(Color.WHITE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private int getTagPosition(LinearLayout linearLayout) {
	    int tag = -1;
        Object tb = linearLayout.getTag();
        if(tb != null && tb instanceof Integer){
            tag = (Integer) tb;
        }
        return tag;
    }

    private String getForMatTime(String zxczsj) {
		if(!TextUtils.isEmpty(zxczsj)){
			//"\/Date(915120000000+0800)\/"
			return zxczsj.replace("\\/","-");
		}

		return "";
	}

	public String getUsableValue(String value){
		return !TextUtils.isEmpty(value)?value:"";
	}

	static class ViewHolder {
		TextView tvYzmc;
		//		TextView tvYzbz;
		TextView tvYzjl;
		TextView tvGyfs;
		TextView tvPl;
		TextView tvTz;
		TextView tvZxsj;
		TextView tvZxjssj;
		TextView tvFs;
		LinearLayout llRoot;
	}
}
